Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### Selecting Fonts Selecting fonts is the third place where our Baby Editor program will use the Common Dialog control. When used as a Font dialog box, the Common Dialog control has a Flags property you set to determine what is displayed in the dialog box. You must specify one of the following flags: • cdlCFPrinterFonts—Displays only printer fonts • cdlCFScreenFonts—Displays only screen fonts • cdlCFBOTH—Displays both screen and printer fonts You can combine one of these flags with cdlCFEffects, which specifies that the Font dialog box should display underline, strikethrough, and color choices as well. Before displaying the Font dialog box, you should load it with the current font settings—that is, the settings currently in effect in the Text Box control. You can accomplish this task easily by copying the relevant properties from one control to the other, before using the ShowFont method to display the font version of the Common Dialog control. When the user closes the dialog box, the program checks the FontName property of the Common Dialog. If this is a blank string, the user has canceled the dialog box, so we exit the procedure without making any changes to the Text Box’s Font property. Otherwise, we reverse the property-copying operation, making the Text Box’s font-related properties equal to the corresponding settings in the Font dialog box. The code for the Edit|Font command’s Click event procedure is shown in Listing 11.15. The font selected applies immediately to all of the text in the editor. The font setting affects only the display, because font information is not saved in the file along with the text. Listing 11.15 The Edit|Font command event procedure.
    Private Sub mnuEditFont_Click()

    ‘ Set flags.
    CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects

    ‘ Set initial values for the dialog box.
    CommonDialog1.FontName = Text1.FontName
    CommonDialog1.FontSize = Text1.FontSize
    CommonDialog1.FontBold = Text1.FontBold
    CommonDialog1.FontItalic = Text1.FontItalic
    CommonDialog1.FontUnderline = Text1.FontUnderline
    CommonDialog1.FontStrikethru = Text1.FontStrikethru
    CommonDialog1.Color = Text1.ForeColor

    ‘ Display Choose Font dialog box.
    CommonDialog1.ShowFont

    ‘ If canceled, exit.
    If CommonDialog1.FontName = “” Then Exit Sub

    ‘ Change the text font according to options selected.
    Text1.FontName = CommonDialog1.FontName
    Text1.FontSize = CommonDialog1.FontSize
    Text1.FontBold = CommonDialog1.FontBold
    Text1.FontItalic = CommonDialog1.FontItalic
    Text1.FontUnderline = CommonDialog1.FontUnderline
    Text1.FontStrikethru = CommonDialog1.FontStrikethru
    Text1.ForeColor = CommonDialog1.Color

    End Sub
#### Exiting The Program To exit the program, we need only the End statement, placing it in the event procedure for the File|Exit command. To ensure that unsaved data is not lost, we call the SaveChanges function. After that, we either exit the sub procedure or end the program, depending on the return value of the function. The code for mnuFileExit_Click is shown in Listing 11.16. Listing 11.16 The File|Exit command event procedure.
    Private Sub mnuFileExit_Click()

    ‘ Verify that changes are saved, if user desires.

    Dim Reply As Integer

    Reply = SaveChanges()
    If Reply = False Then Exit Sub

    End

    End Sub
That’s it for the Baby Editor—it’s all finished. Along the way, we have learned a lot about text processing, as well as about other aspects of Visual Basic, such as menus, functions, and file operations. I want to show you one more thing before closing this chapter. ### Using Multiple Forms As I have mentioned several times previously, one of Visual Basic’s strong points is its ability to create and use software components. It’s time to take a look at one way this is handled. Believe it or not, you have just created a software component—your Baby Editor. The Calculator we built in a previous chapter is another component. A Visual Basic form, in other words, is one type of software component. How do you incorporate multiple forms in a project? It’s really quite easy. A project can have as many forms as needed. To add a form to an existing project, select Add Form from the Project menu. Then do either of the following: • To add a new, blank form—Click on the New tab in the dialog box, click on the Form icon, then select Open. • To add a form that already exists as part of another project—Click on the Existing tab and browse to locate the form file on disk. When a project contains multiple forms, they will be listed in the Project Explorer window. To select one of these forms, click on its name, then click on either the View Object or View Code button (depending on whether you want to work on the form’s controls or its code). No matter how many forms a project might have, only one can be shown first, when the program begins executing. This startup form is normally the first form created during project development. You can change the startup form by selecting Properties from the Project menu and clicking on the General tab in the dialog box that is displayed (Figure 11.6). Pull down the Startup Object list, and you’ll see a list of your project’s forms. Select the one that is to be the startup form. How do you switch from one form to another? In other words, how do you control which of your project’s forms are visible and which are not? Each Form object has a Show method that causes it to be displayed. The syntax is
    FormName.Show style
where FormName is the form’s Name property and style is an integer specifying the type of form—modal (style = 1) or modeless (style = 0 or style omitted). When a modal form is displayed, no keyboard or mouse input can occur for the application except to the modal form. This means the modal form must be hidden (usually in response to user action) before the user can activate or use any other form belonging to the same application. A modal form does not, however, prevent the user from activating or using another application. In contrast, a modeless form does not place these restrictions on the user. Display of a modeless form does not prevent the user from activating another of the program’s forms. Thus, if a Visual Basic program has several forms displayed and one of them is modal, only the modal form will accept user input, even though the others are visible. If none of the forms is modal, the user can freely switch among them. Generally, you will use modeless forms unless you have a specific reason to use a modal form. Figure 11.6 The General tab of the Project Properties dialog box. To hide a form, use its Hide method. Note that using the Show and Hide methods has the same effect as setting the form’s Visible property to True or False.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.